rm(list=ls(all=TRUE))

path <- "" #here you need to adjust the path macro to the place on your computer
setwd(path)

library(haven)
library(dplyr)
library(ggplot2)
library(Hmisc)
library(ggrepel)

dat <- read_dta(paste0(path, "\\party_governments.dta"))

dat$iso2code <- as_factor(dat$iso2code)
dat$country <- as_factor(dat$country)

# adjusting the sample, dropping parties which have not sponsored 
# protests and cabinets under which no protest has happened.

table(dat$country)
length(unique(dat$country))

# adding some new variables

dat$in_parl[dat$seat_sh_bef<=0] <- 0
dat$in_parl[dat$seat_sh_bef>0] <- 1
table(dat$in_parl)

dat <- dat %>% 
  group_by(iso2code, cabinet_name) %>% 
  mutate(largest= ifelse(vote_bef==max(vote_bef), 1, 0))

dim(plyr::count(dat[dat$largest==1,], vars = c("iso2code", "cabinet_name")))
dim(plyr::count(dat, vars = c("iso2code", "cabinet_name")))

dat$large[dat$vote_bef>=25] <- 1
dat$large[dat$vote_bef<25] <- 0

dat$mid_size[dat$vote_bef>=10 & dat$vote_bef<25] <- 1
dat$mid_size[dat$vote_bef<10 | dat$vote_bef>=25] <- 0

dat$small[dat$vote_bef<10] <- 1
dat$small[dat$vote_bef>10] <- 0

summary(dat$left_right)
summary(dat$cab_leftright)
summary(dat$state_market)
summary(dat$liberty_authority)

dat$left_prt[dat$left_right<5] <- 1
dat$left_prt[dat$left_right>=5] <- 0

dat$left_cab[dat$cab_leftright<5] <- 1
dat$left_cab[dat$cab_leftright>=5] <- 0

dat$eleft_prt[dat$state_market<5] <- 1
dat$eleft_prt[dat$state_market>=5] <- 0

dat$cleft_prt[dat$liberty_authority<5] <- 1
dat$cleft_prt[dat$liberty_authority>=5] <- 0

dat$two_gov[dat$gov_counter>=2] <- 1
dat$two_gov[dat$gov_counter<2] <- 0

dat$one_gov[dat$gov_counter>=1] <- 1
dat$one_gov[dat$gov_counter<1] <- 0

dat$past_pm[dat$pm_counter>=1] <- 1
dat$past_pm[dat$pm_counter<1 | is.na(dat$pm_counter)] <- 0

dat$mainstream[dat$family2==2 | dat$family2==4 | dat$family2==6 | dat$family2==8] <- 1
dat$mainstream[is.na(dat$mainstream) & !is.na(dat$family2)] <- 0
table(dat$mainstream)

cab_cntr <-  dat %>% 
  group_by(iso2code, cabinet_name) %>%
  summarise_at(vars(bmr_demdur, vdem_civil_soc,
                    all_evts, all_parties_evts), funs(mean(., na.rm=TRUE)))

cab_cntr <- within(cab_cntr, prt_share <- all_parties_evts*100/all_evts)
table(cab_cntr$prt_share)
cab_cntr$cabinet_name[cab_cntr$prt_share>100]
quantile(cab_cntr$prt_share, na.rm=TRUE)[3]
mean(cab_cntr$prt_share, na.rm=TRUE)

cab_cntr$ids <- paste0(cab_cntr$cabinet_name, ' (', cab_cntr$iso2code, ')')


# low and high contexts

cab_cntr$civil_soc_cat <- cut2(cab_cntr$vdem_civil_soc, g=2)
table(cab_cntr$civil_soc_cat, cab_cntr$iso2code)

# differentiated countries

diff.cntry <- c("BE" ,"DK" ,"FI" ,"FR" ,"GR" ,"IE" ,"IT" ,"LU" ,"MT" ,"NL" ,"NO" ,"PT" ,"RO" ,"SI" ,"UK")
nodiff.cntry <- c("AT" ,"BG" ,"CY" ,"CZ" ,"EE" ,"DE" ,"HU" ,"IS" ,"LV" ,"LT" ,"PL" ,"SK" ,"ES" ,"SE" ,"CH")

cab_cntr$prt_share_cat[as.character(cab_cntr$iso2code) %in% diff.cntry] <- 0
cab_cntr$prt_share_cat[as.character(cab_cntr$iso2code) %in% nodiff.cntry] <- 1

cab_cntr$prt_share_cat <- relevel(as.factor(cab_cntr$prt_share_cat), "0")

dat_upper_lev_cat <- subset(cab_cntr, select=c("iso2code",
                                               "cabinet_name",
                                               "civil_soc_cat",
                                               "prt_share",
                                               "prt_share_cat"))
dat <- merge(dat, dat_upper_lev_cat)
rm(dat_upper_lev_cat, cab_cntr)

######################
###### Figure 1 ######
######################

avrg_sh <- dat %>%
  group_by(civil_soc_cat) %>% 
  mutate(all_evts=sum(all_evts),
         all_parties_evts=sum(all_parties_evts)) %>% 
  select(all_evts, all_parties_evts) %>% 
  distinct() 
head(avrg_sh)

avrg_sh <- within(avrg_sh, prt_share <- all_parties_evts*100/all_evts)
avrg_sh <- avrg_sh[complete.cases(avrg_sh),]

ggplot(avrg_sh, aes(x=civil_soc_cat, y=prt_share)) +
  geom_bar(stat="identity") +
  ylab("Share of Party Sponsored Protests") +
  scale_x_discrete(labels=c("[0.626,0.925)" = "Low [0.626,0.925)",
                            "[0.925,0.976]" = "High [0.925,0.976]")) +
  xlab("Strength of Civil Society") +
  theme_bw()


reg_sh <- dat %>%
  group_by(region) %>% 
  mutate(all_evts=sum(all_evts),
         all_parties_evts=sum(all_parties_evts)) %>% 
  select(all_evts, all_parties_evts) %>% 
  distinct() 
head(reg_sh)

reg_sh <- within(reg_sh, prt_share <- all_parties_evts*100/all_evts)
reg_sh$region <- as_factor(reg_sh$region)
reg_sh <- reg_sh[complete.cases(reg_sh),]


ggplot(reg_sh, aes(x=region, y=prt_share)) +
  geom_bar(stat="identity") +
  ylab("Share of Party Sponsored Protests") +
  xlab("Region") +
  theme_bw()

avrg_sh <- subset(avrg_sh, select = c("civil_soc_cat", "prt_share"))
names(avrg_sh)[1] <- "name"
avrg_sh$name <- as.character(avrg_sh$name)
avrg_sh <- as.data.frame(avrg_sh)
reg_sh <- subset(reg_sh, select = c("region", "prt_share"))
names(reg_sh)[1] <- "name"
reg_sh$name <- as.character(reg_sh$name)
reg_sh <- as.data.frame(reg_sh)
avrg_sh <- rbind(avrg_sh, reg_sh)
rm(reg_sh)
avrg_sh$name[avrg_sh$name=="[0.626,0.925)"] <- "Low"
avrg_sh$name[avrg_sh$name=="[0.925,0.976]"] <- "High"
avrg_sh$name[avrg_sh$name=="North-Western Europe"] <- "Northwestern Europe"

avrg_sh$facet <- as.character("")

avrg_sh[1:2, ]$facet <- "Civil Society"
avrg_sh[3:5, ]$facet <- "Democratization"

avrg_sh$name <- factor(avrg_sh$name, levels = c("Low", "High",
                                "Northwestern Europe",
                                "Southern Europe",
                                "Eastern Europe"))

ggplot(avrg_sh, aes(x=name, y=prt_share)) +
  geom_bar(stat="identity") +
  facet_wrap(~facet, scales = "free_x") +
  xlab("") + ylab("Share of party protests") +
  scale_x_discrete(labels=c("Northwestern Europe" = "Northwestern\nEurope",
                            "Southern Europe" = "Southern\nEurope",
                            "Eastern Europe" = "Eastern\nEurope")) +
  theme_bw()
  # theme(axis.text.x = element_text(angle = 30, hjust = 1))

ggsave(paste0(path, "paper\\revision\\empirical_results\\October_2019\\figures\\f1_context.eps"),
       plot=last_plot(), dpi=200, scale=0.6, width=8, height=6)

ggsave(paste0(path, "paper\\revision\\empirical_results\\October_2019\\figures\\png\\f1_context.png"),
       plot=last_plot(), dpi=200, scale=0.6, width=8, height=6)

rm(avrg_sh)

##################################
###### Figure 1, appendix E ######
##################################

dat$civil_soc_cat <- ifelse(as.character(dat$civil_soc_cat)=="[0.626,0.925)", "Low CS", "High CS")
dat$region <- ifelse(as.character(as_factor(dat$region))=="North-Western Europe", "NWE",
       ifelse(as.character(as_factor(dat$region))=="Southern Europe", "SE","EE")) 

dat <- transform(dat, civ_reg=paste(region, civil_soc_cat, sep = " "))

dat$civ_reg <- as_factor(dat$civ_reg)
table(dat$civ_reg)

reg_sh <- dat %>%
  group_by(civ_reg) %>% 
  select(civ_reg, all_evts, all_parties_evts) %>% 
  distinct() %>% 
  mutate(all_evts=sum(all_evts),
         all_parties_evts=sum(all_parties_evts)) %>% 
  distinct() %>% 
  summarise_at(vars(all_evts, all_parties_evts), funs(mean(., na.rm=TRUE)))
head(reg_sh)

reg_sh <- within(reg_sh, prt_share <- all_parties_evts*100/all_evts)
reg_sh$civ_reg <- factor(reg_sh$civ_reg, levels = c("NWE Low CS", "NWE High CS", "SE Low CS", 
                                  "SE High CS", "EE Low CS", "EE High CS"))

reg_sh <- reg_sh[!is.na(reg_sh$civ_reg),]

ggplot(reg_sh, aes(x=civ_reg, y=prt_share)) +
  geom_bar(stat="identity") +
  xlab("") + ylab("Share of Party Sponsored Protests") +
  theme_bw()

ggsave(paste0(path, "\\context_inter.eps"),
       plot=last_plot(), dpi=200, scale=0.6, width=10, height=6)
ggsave(paste0(path, "\\context_inter.png"),
       plot=last_plot(), dpi=200, scale=0.6, width=10, height=6)
rm(reg_sh)


######################
###### Figure 5 ######
######################

#mainstream

hdat1.1 <- dat %>%
  filter(!is.na(civil_soc_cat) & !is.na(mainstream)) %>% 
  group_by(civil_soc_cat, mainstream) %>% 
  summarise_at(vars(parties_evts), funs(sum(., na.rm=TRUE)))
hdat1.2 <- dat %>%
  filter(!is.na(civil_soc_cat) & !is.na(mainstream)) %>% 
  group_by(civil_soc_cat) %>% 
  summarise(group_tot=sum(parties_evts, na.rm=TRUE))
hdat1 <- merge(hdat1.1, hdat1.2)
hdat1 <- hdat1 %>% 
  mutate(sh= case_when(mainstream==1 ~ parties_evts*100/group_tot)) %>% 
  select(civil_soc_cat, sh) %>% 
  filter(complete.cases(.))
hdat1 <- t(hdat1[,2])
colnames(hdat1) <- c("low civ", "high civ")
print(hdat1)
hdat2.1 <- dat %>%
  filter(!is.na(region) & !is.na(mainstream)) %>% 
  group_by(region, mainstream) %>% 
  summarise_at(vars(parties_evts), funs(sum(., na.rm=TRUE)))
hdat2.2 <- dat %>%
  filter(!is.na(region) & !is.na(mainstream)) %>% 
  group_by(region) %>% 
  summarise(group_tot=sum(parties_evts, na.rm=TRUE))
hdat2 <- merge(hdat2.1, hdat2.2)
hdat2 <- hdat2 %>% 
  mutate(sh= case_when(mainstream==1 ~ parties_evts*100/group_tot)) %>% 
  select(region, sh) %>% 
  filter(complete.cases(.))
hdat2 <- t(hdat2[,2])
colnames(hdat2) <- c("NWE", "SE", "EE")
print(hdat2)
hdat3.1 <- dat %>%
  filter(!is.na(prt_share_cat) & !is.na(mainstream)) %>% 
  group_by(prt_share_cat, mainstream) %>% 
  summarise_at(vars(parties_evts), funs(sum(., na.rm=TRUE)))
hdat3.2 <- dat %>%
  filter(!is.na(prt_share_cat) & !is.na(mainstream)) %>% 
  group_by(prt_share_cat) %>% 
  summarise(group_tot=sum(parties_evts, na.rm=TRUE))
hdat3 <- merge(hdat3.1, hdat3.2)
hdat3 <- hdat3 %>% 
  mutate(sh= case_when(mainstream==1 ~ parties_evts*100/group_tot)) %>% 
  select(prt_share_cat, sh) %>% 
  filter(complete.cases(.))
hdat3 <- t(hdat3[,2])
colnames(hdat3) <- c("low pp", "high pp")
print(hdat3)
table <- cbind("mainstream", hdat1, hdat2, hdat3)
rm(list=ls(pattern="hdat"))


#past_pm

hdat1.1 <- dat %>%
  filter(!is.na(civil_soc_cat) & !is.na(past_pm)) %>% 
  group_by(civil_soc_cat, past_pm) %>% 
  summarise_at(vars(parties_evts), funs(sum(., na.rm=TRUE)))
hdat1.2 <- dat %>%
  filter(!is.na(civil_soc_cat) & !is.na(past_pm)) %>% 
  group_by(civil_soc_cat) %>% 
  summarise(group_tot=sum(parties_evts, na.rm=TRUE))
hdat1 <- merge(hdat1.1, hdat1.2)
hdat1 <- hdat1 %>% 
  mutate(sh= case_when(past_pm==1 ~ parties_evts*100/group_tot)) %>% 
  select(civil_soc_cat, sh) %>% 
  filter(complete.cases(.))
hdat1 <- t(hdat1[,2])
colnames(hdat1) <- c("low civ", "high civ")
print(hdat1)
hdat2.1 <- dat %>%
  filter(!is.na(region) & !is.na(past_pm)) %>% 
  group_by(region, past_pm) %>% 
  summarise_at(vars(parties_evts), funs(sum(., na.rm=TRUE)))
hdat2.2 <- dat %>%
  filter(!is.na(region) & !is.na(past_pm)) %>% 
  group_by(region) %>% 
  summarise(group_tot=sum(parties_evts, na.rm=TRUE))
hdat2 <- merge(hdat2.1, hdat2.2)
hdat2 <- hdat2 %>% 
  mutate(sh= case_when(past_pm==1 ~ parties_evts*100/group_tot)) %>% 
  select(region, sh) %>% 
  filter(complete.cases(.))
hdat2 <- t(hdat2[,2])
colnames(hdat2) <- c("NWE", "SE", "EE")
print(hdat2)
hdat3.1 <- dat %>%
  filter(!is.na(prt_share_cat) & !is.na(past_pm)) %>% 
  group_by(prt_share_cat, past_pm) %>% 
  summarise_at(vars(parties_evts), funs(sum(., na.rm=TRUE)))
hdat3.2 <- dat %>%
  filter(!is.na(prt_share_cat) & !is.na(past_pm)) %>% 
  group_by(prt_share_cat) %>% 
  summarise(group_tot=sum(parties_evts, na.rm=TRUE))
hdat3 <- merge(hdat3.1, hdat3.2)
hdat3 <- hdat3 %>% 
  mutate(sh= case_when(past_pm==1 ~ parties_evts*100/group_tot)) %>% 
  select(prt_share_cat, sh) %>% 
  filter(complete.cases(.))
hdat3 <- t(hdat3[,2])
colnames(hdat3) <- c("low pp", "high pp")
print(hdat3)
table <- rbind(table, cbind("past_pm", hdat1, hdat2, hdat3))
rm(list=ls(pattern="hdat"))

#opp_party

hdat1.1 <- dat %>%
  filter(!is.na(civil_soc_cat) & !is.na(opp_party)) %>% 
  group_by(civil_soc_cat, opp_party) %>% 
  summarise_at(vars(parties_evts), funs(sum(., na.rm=TRUE)))
hdat1.2 <- dat %>%
  filter(!is.na(civil_soc_cat) & !is.na(opp_party)) %>% 
  group_by(civil_soc_cat) %>% 
  summarise(group_tot=sum(parties_evts, na.rm=TRUE))
hdat1 <- merge(hdat1.1, hdat1.2)
hdat1 <- hdat1 %>% 
  mutate(sh= case_when(opp_party==1 ~ parties_evts*100/group_tot)) %>% 
  select(civil_soc_cat, sh) %>% 
  filter(complete.cases(.))
hdat1 <- t(hdat1[,2])
colnames(hdat1) <- c("low civ", "high civ")
print(hdat1)
hdat2.1 <- dat %>%
  filter(!is.na(region) & !is.na(opp_party)) %>% 
  group_by(region, opp_party) %>% 
  summarise_at(vars(parties_evts), funs(sum(., na.rm=TRUE)))
hdat2.2 <- dat %>%
  filter(!is.na(region) & !is.na(opp_party)) %>% 
  group_by(region) %>% 
  summarise(group_tot=sum(parties_evts, na.rm=TRUE))
hdat2 <- merge(hdat2.1, hdat2.2)
hdat2 <- hdat2 %>% 
  mutate(sh= case_when(opp_party==1 ~ parties_evts*100/group_tot)) %>% 
  select(region, sh) %>% 
  filter(complete.cases(.))
hdat2 <- t(hdat2[,2])
colnames(hdat2) <- c("NWE", "SE", "EE")
print(hdat2)
hdat3.1 <- dat %>%
  filter(!is.na(prt_share_cat) & !is.na(opp_party)) %>% 
  group_by(prt_share_cat, opp_party) %>% 
  summarise_at(vars(parties_evts), funs(sum(., na.rm=TRUE)))
hdat3.2 <- dat %>%
  filter(!is.na(prt_share_cat) & !is.na(opp_party)) %>% 
  group_by(prt_share_cat) %>% 
  summarise(group_tot=sum(parties_evts, na.rm=TRUE))
hdat3 <- merge(hdat3.1, hdat3.2)
hdat3 <- hdat3 %>% 
  mutate(sh= case_when(opp_party==1 ~ parties_evts*100/group_tot)) %>% 
  select(prt_share_cat, sh) %>% 
  filter(complete.cases(.))
hdat3 <- t(hdat3[,2])
colnames(hdat3) <- c("low pp", "high pp")
print(hdat3)
table <- rbind(table, cbind("opp_party", hdat1, hdat2, hdat3))
rm(list=ls(pattern="hdat"))

# arrange table

colnames(table)[1] <- "name"
table <- as.data.frame(table)

table[,-1] <- sapply(table[,-1],as.character)
table[,-1] <- sapply(table[,-1],as.numeric)

table <- data.frame(table)

in_paper_r <- c("mainstream", "past_pm", "opp_party")

table <- subset(table, name %in% in_paper_r, select=c("name", "low.pp", "high.pp"))

table <- reshape2::melt(table, id.vars="name")

table$variable <- as.character(table$variable)

table$variable[table$variable=="low.pp"] <- "High Diff."
table$variable[table$variable=="high.pp"] <- "Low Diff."

table$variable <- factor(table$variable, levels =  c("Low Diff.", "High Diff."))

table$name <- as.character(table$name)
table$name[table$name=="opp_party"] <- "Opposition Party"
table$name[table$name=="past_pm"] <- "Previously PM Party"
table$name[table$name=="mainstream"] <- "Mainstream Party Family"

table$name <- factor(table$name, levels = c("Mainstream Party Family",
                              "Previously PM Party",
                              "Opposition Party"))

ggplot(table, aes(x=variable, y=value, fill=variable)) +
  geom_bar(stat="identity") +
  facet_wrap(~name) +
  xlab("") + ylab("Share in party protests (pp.)") +
  scale_fill_manual(values=c("#2a2b2d", "#7f8082")) +
  guides(fill=FALSE) +
  theme_bw()

ggsave(paste0(path, "\\f2_party_type.eps"),
       plot=last_plot(), dpi=200, scale=0.6, width=8, height=5)

ggsave(paste0(path, "\\f2_party_type.png"),
       plot=last_plot(), dpi=200, scale=0.6, width=8, height=5)

